package com.business.action.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.business.bean.oa.WorkSupply;
import com.business.bean.system.UserBean;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.util.WebUtils;

/**
 * 考勤补漏
 * 
 * @author fengjian
 *
 */
@Controller
@RequestMapping("/oa/supply")
public class SupplyAction {

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/page.do")
	public PageBean page(HttpServletRequest request, Service service) {
		PageBean page = WebUtils.getPageBean(request);
		Map<String, Object> map = WebUtils.getRequestData(request);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setPage(page);
		query.setEntityClass(WorkSupply.class);
		query.setTableName("OA_WORK_SUPPLY_VIEW");
		query.createSql(map);
		dao.list();
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/save.do")
	public PageBean save(Service service, WorkSupply bean) {
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(UserBean.class);
		query.createSql(bean.getUserID());
		UserBean user = dao.get();
		bean.setCheckUserID(user.getLeaderID());
		if (DBUtils.update(service, bean) > 0) {
			res.setMessage("保存信息成功！");
		} else {
			res.setSuccess(false);
			res.setMessage("保存信息失败！");
		}
		return res;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	public PageBean delete(Service service, String supplyID) {
		return DBUtils.delete(service, WorkSupply.class, supplyID);
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/check.do")
	public PageBean check(Service service, int status, String userID, String ids) {
		PageBean res = new PageBean();
		String[] arr = StringUtils.split(ids, '.');
		Date d = new Date();
		java.sql.Timestamp today = new java.sql.Timestamp(d.getTime());
		List<WorkSupply> list = new ArrayList<>();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(WorkSupply.class);
		query.setSQL("select * from OA_WORK_SUPPLY where supplyID=?");
		for (String id : arr) {
			query.addParameter(id);
			WorkSupply w = dao.get();
			w.setStatus(status);
			w.setCheckUserID(userID);
			w.setCheckTime(today);
			list.add(w);
			query.clearParameterList();
		}
		query.createBatchUpdateSql("status,checkTime,checkUserID", list);
		int[] is = dao.batchUpdate();
		if (dao.getBatchUpdateRecordCount() > 0) {
			// 如果是审批通过
			if (status == 1) {
				/* WorkDayReportService wrs = new WorkDayReportService(service);
				for (int i = 0; i < list.size(); i++) {
					if (is[i] > 0) {
						WorkSupply bean = list.get(i);
						wrs.userSupplyAction(bean);
					}
				} */
			}
			// 如果是审批不通过
			else if (status == 2) {
				query.setSQL("delete from DEV_FACE_RECORD where applyID=?");
				for (int i = 0; i < list.size(); i++) {
					query.addParameter(list.get(i).getSupplyID());
					dao.update();
					query.clearParameterList();
				}
			}
			res.setMessage("保存信息成功！");
		} else {
			res.setSuccess(false);
			res.setMessage("保存信息失败！");
		}
		return res;
	}

}
